<?php
/*
 * @Author: 偻儸小卒[EdisonLiu_]
 * @Date: 2021-06-23 15:05:51
 * @LastEditors: 偻儸小卒[EdisonLiu_]
 * @LastEditTime: 2021-06-24 14:32:14
 * @Description: 
 */

namespace App\Http\Controllers\AdminApi\Member;

use App\Http\Controllers\Controller;
use App\Http\Models\Admin\ClassifyPermissions;

use App\Http\Models\Admin\Member\Street;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StreetController extends Controller
{
    // 查列表
    public function list(Request $request)
    {

        $limit = $request->get('limit');
        $title = $request->get('title');
        $sort = getSort($request);
        // var_dump($sort);die;
        // 
        $items = Street::orderBy($sort[0], $sort[1])->paginate($limit);

        return response()->json(['code' => 20000, 'message' => '角色查询成功', 'data' => $items]);
    }
    // 查一条
    public function getinfo(Request $request)
    {
        $id = $request->get('id');
        // redaySql(new DB);
        $Street = Street::find($id);
        // showSql(new DB);
        return response()->json(['code' => 20000, 'message' => '督查员查询成功', 'data' => compact('Street')]);
    }
    // 创建
    public function create(Request $request)
    {
        $Street = new Street;


        $Street->username = $request->get('username');

        // 判断用户名是否存在
        $is_street = $Street->where([['username', '=', $Street->username]])->first();
        if (!empty($is_street)) {
            return response()->json(['code' => 10001, 'message' => '该用户名已存在']);
        }
        $Street->title = $request->get('title');
        $Street->address = $request->get('address');
        $Street->ascription = 2;
        $Street->realname = $request->get('realname');
        $Street->mobile = $request->get('mobile');
        $Street->password = $request->get('password');

        $Street->save();

        return response()->json(['code' => 20000, 'message' => '修改督查员成功', 'data' => compact('Street')]);
    }

    //修改
    public function update(Request $request)
    {

        $id = $request->get('id');
        if (empty($id)) {
            return response()->json(['code' => 10000, 'message' => '查无此人']);
        }
        $Street = Street::findOrFail($id);
        if (empty($Street)) {
            return response()->json(['code' => 10000, 'message' => '查无此人']);
        }
        $req_username = $request->get('username');

        // 如果该人员原有的用户名不是当前提交的用户名，并且当前用户名在数据库中已存在，则提示用户重名
        $is_street = $Street->where([['username', '=', $req_username]])->count();
        if (($is_street == 1 && $Street->username != $req_username) || $is_street > 1) {
            return response()->json(['code' => 10001, 'message' => '该用户名已存在']);
        }
        $Street->title = $request->get('title');
        $Street->username = $req_username;
        $Street->address = $request->get('address');
        $Street->ascription = 2;
        $Street->realname = $request->get('realname');
        $Street->mobile = $request->get('mobile');
        $Street->password = $request->get('password');

        $Street->update();

        // dd($ress);die;
        return response()->json(['code' => 20000, 'message' => '修改督查员成功', 'data' => compact('Street')]);
    }
    // 删除
    public function delete(Request $request)
    {
        $id = $request->get('id');
        $Street = Street::findOrFail($id);
        $Street->delete();
        return response()->json(['code' => 20000, 'message' => '删除成功', 'data' => $id]);
    }
}
